﻿using System.Collections.Generic;

namespace GVPSys.sysbase
{
    internal interface ICommandAdvisor
    {
        ICommandInterceptor Interceptor { get; }
        IList<ICommandThrowsInterceptor> ExceptionInterceptors { get; }

        IList<IBeforeCommand> Befores { get; }
        IList<IAfterCommand> Afters { get; }

        void AddAdvised(ICommandAdvised advised);
    }

    internal class CommandAdvisor : ICommandAdvisor
    {
        public ICommandInterceptor Interceptor { get; private set; }
        public IList<ICommandThrowsInterceptor> ExceptionInterceptors { get; private set; }
        public IList<IBeforeCommand> Befores { get; private set; }
        public IList<IAfterCommand> Afters { get; private set; }

        public void AddAdvised(ICommandAdvised advised)
        {
            if (advised is ICommandInterceptor)
            {
                Interceptor = (ICommandInterceptor)advised;
            }
            if (advised is ICommandThrowsInterceptor)
            {
                if (ExceptionInterceptors == null)
                {
                    ExceptionInterceptors = new List<ICommandThrowsInterceptor>();
                }
                ExceptionInterceptors.Add((ICommandThrowsInterceptor)advised);
            }
            if (advised is IBeforeCommand)
            {
                if (Befores == null)
                {
                    Befores = new List<IBeforeCommand>();
                }
                Befores.Add((IBeforeCommand)advised);
            }
            if (advised is IAfterCommand)
            {
                if (Afters == null)
                {
                    Afters = new List<IAfterCommand>();
                }
                Afters.Add((IAfterCommand)advised);
            }
        }
    }
}
